<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return=array();
	exi($return);
}
if($_RQ['op']=='getgoods'){
	$merchid =intval($_RQ['merchid'])?:$_SESSION['onemerchid'];
	if(!$merchid && $_RQ['frommerch']){
		$install = check_account_permission('jy_merch');
		if($install){	
			$merchid = pdo_getcolumn('jy_merch_user',array('mid'=>$_SESSION['uid']),'id');
		}
		$merchid=$merchid?:'-1';
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'merchid'=>$merchid,'nochecked'=>0,'type <>'=>4);
	if($_RQ['keyword']){		
			$params['title like']='%'.trim($_RQ['keyword']).'%';
			$return['params']['title']=$_RQ['keyword'];	
	}
	if($_RQ['pcate']){
		$params['pcate2']=intval($_RQ['pcate']);
	}
	if($_RQ['cid']){
		$params['ccate']=intval($_RQ['cid']);
	}
	if($_RQ['order']=='isrecommand'){
		$params['saletype likes']=array('1');
	}
	if($_RQ['order']=='isnew'){
		$params['saletype likes']=array('2');
	}
	if($_RQ['order']=='ishot'){
		$params['saletype likes']=array('3');
	}
	if($_RQ['order']=='isdiscount'){
		$params['isdiscount']=intval($_RQ['isdiscount']);
	}
	if($_RQ['order']=='issendfree'){
		$params['issendfree']=intval($_RQ['issendfree']);
	}
	if($_RQ['order']=='istime'){
		$params['istime']=intval($_RQ['istime']);
	}
	if(!$merchid && $return['shopset']['no_showmerch']==1){
		$params['merchid']=0;
	}
	//按点赞数排序作品
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=intval($_RQ['num']);
	}
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','salenum desc, id desc');
	if(!$list){
		//exi('none');
	}
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		}
	}
	
	exi($list);
}
if($_RQ['op']=='getmerchs'){
	//获取最近50家店
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng');
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'merchname <>'=>'',
		'lat_lng <>'=>'',
	);
	if($_RQ['cid']){
		$params['cateid']=intval($_RQ['cid']);
		$return['params']['cid']=intval($_RQ['cid']);
	}
	$orderby='isrecommand desc, salenum desc, id desc';
	if(trim($_RQ['order']) && trim($_RQ['order'])!='all'){
		$order=trim($_RQ['order']);
        if($order=='salenum' || $order=='sales'){
			$orderby='salenum desc, isrecommand desc, id desc';
			$orderbykey='salenum';
		}
		if($order=='sudu'){
			$orderby='sudu desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='sudu';
		}
		if($order=='star'){
			$orderby='star desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='star';
		}
		if($order=='brand'){
			$params['isbrand']=1;
		}
		if($order=='you'){
			$orderby='isrecommand desc, salenum desc, id desc';
			$orderbykey='isrecommand';
		}
		if($order=='out_pay'){
			$orderby='out_pay desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='out_pay';
		}
		if($order=='out_price'){
			$orderby='out_price desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='out_price';
		}
		$return['params']['order']=trim($_RQ['order']);
	}
	if(intval($_RQ['star'])){
		$params['star >']=intval($_RQ['star'])-0.2;
		$return['params']['star']=intval($_RQ['star']);
	}
	if(trim($_RQ['keyword'])){
		$params['merchname like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	$pindex = max(1, 1);
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,10000),//获取所有
		'fields'=>array('id','merchname','out_price','lat_lng','logo','topimg','cateid','onlinetime'),
		'key'=>'id',
		'orderby'=>$orderby,
		'orderbykey'=>$orderbykey,
	);
	$merchs=model('jy_merch/model')->getmerch($parameter,$_RQ['limit'],$lat_lng,intval($_RQ['page']),intval($_RQ['juli']));
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	foreach($merchs as $k=>&$v){
			if(!$v['goodsnum']){
				$v['goodsnum']=pdo_count('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$v['id'],'status'=>1,'nochecked'=>0),'id');
			}
			$v['logo']=$v['logo']?tomedia($v['logo']):'';
			$v['topimg']=$v['topimg']?tomedia($v['topimg']):'';
			$v['onlinetime']=json_decode($v['onlinetime'],true);
			$v['onlinetime']['start']=$v['onlinetime']['start']?:'7:00';
			$v['onlinetime']['end']=$v['onlinetime']['end']?:'22:00';	
			$days=date('Y-m-d',TIMESTAMP);
			$star=strtotime($days.' '.$v['onlinetime']['start']);
			$end=strtotime($days.' '.$v['onlinetime']['end']);
			if($star>TIMESTAMP || TIMESTAMP>$end){
				$v['offline']=1;
			}
			if($v['juli']<10){
				$v['peisongtime']=$v['juli']*5+28;
			}else if($v['juli']<30){
				$v['peisongtime']=$v['juli']*3+28;
			}else{
				$v['peisongtime']=$v['juli']*1+28;
			}
			$v['out_juli']=$v['out_juli']>0?$v['out_juli']:8;
			$v['out_price']=$v['out_price']>0?$v['out_price']:$citypeisong['out_price'];
			$pindex = max(1, 1);
			$psize = 6;
			$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'nochecked'=>0,'merchid'=>$v['id']);
			$fields=array('id','title','price','image','unit','originalprice','nochecked');
			$goods=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','salenum desc, id desc');
			foreach($goods as &$gv){
					$gv['image']=tomedia($gv['image']);
					$gv['originalprice']=$gv['originalprice']?:$gv['price']*1.2;
					$gv['originalprice']=price($gv['originalprice']);
					$v['tags']=pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$gv['id']),'tags');
			}
			$v['goods']=$goods;
			$v['coupons']=model('jy_weishop/coupon')->getmerchcoupon($v['id'],1);
            $salenum=pdo_getsum('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$v['id']),'total');
            if($salenum!=$v['salenum']){
                $v['salenum']=$salenum;
                pdo_update('jy_merch_user',array('salenum'=>$salenum),array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['id']));
            }
			
	}
	$return['list']=array_values($merchs);
	exi($return);
}
if($_RQ['op']=='out_time'){
	$merch_id=intval($_RQ['merch_id']);
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	$post['set_timebox']=$citypeisong['set_timebox'];
	$post['peisong_timebox']=$citypeisong['peisong_timebox'];
	$post['out_pay']=$_RQ['out_pay'];
	$out_time = $this->model('model')->out_times($merch_id,$post);
	$timestamp = TIMESTAMP + 60 * 30;
	$data = model('jy_weishop/store')->array_order($timestamp, $out_time['timestamp']);
	$predict_index = array_search($data, $out_time['timestamp']);
	$predict_day = $out_time['days'][0];
	$predict_time = $out_time['times'][$predict_index]['start'].'~'.$out_time['times'][$predict_index]['end'];
	$text_time = "尽快上门";
	$return['timestamp']=$timestamp;
	$return['predict_time']=$predict_time;
	$return['out_time']=$out_time;
	exi($return);
}
if($_RQ['op']=='getnew'){
	$params=array(
		'status'=>1,
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>intval($_RQ['merchid']),
		'api_pluginname'=>$_RQ['api_pluginname']?:'',
		'send_status'=>array(0,11),
	);
	$order=pdo_get('jy_weishop_order',$params);
	if($order['id']){
		if(!$order['send_status']){
			pdo_update('jy_weishop_order',array('send_status'=>12),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
		}
		exi($order);
	}else{
		exi('none');
	}
}
//获取小程序订阅消息ID
if($_RQ['op']=='get_tmpids'){
	$tmplIds=array();
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('1818',[6,10,11,5,2],'pay2buyer','点餐成功通知','jy_merch');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('27464',[1,2,3,4,5],'congzhi','充值结果通知','jy_merch');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('1984',[9,1,5,6,8],'change','账户余额通知','jy_merch');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$res['list']=$tmplIds;
	exi($res);
}
if($_RQ['op']=='get_tmpids2'){
	$tmplIds=array();
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('1818',[6,10,11,5,2],'pay2buyer','点餐成功通知','jy_merch');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$res['list']=$tmplIds;
	exi($res);
}
if($_RQ['op']=='get_tmpids3'){
	$tmplIds=array();
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('27464',[1,2,3,4,5],'congzhi','充值结果通知','jy_merch');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('1984',[9,1,5,6,8],'change','账户余额通知','jy_merch');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$res['list']=$tmplIds;
	exi($res);
}

if($_RQ['op']=='get_tmpids4'){
	$tmplIds=array();
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('44331',[1,2,3,4],'birth','生日祝福提醒','jy_merch');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('49640',[1,2,3,4],'dqcoupon','优惠券即将到期提醒','jy_merch');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('35936',[1,2,3,5,6],'dzcoupon','优惠券到账通知','jy_merch');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$res['list']=$tmplIds;
	exi($res);
}
?>